﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;

namespace Common.Utils
{
   public class WebApiServie
    {
        //47.112.174.103
        private string BaseUri = "http://47.112.174.103:8080/robot/";
        //private string BaseUri = "http://192.168.3.120:8080/robot/";
        //private string BaseUri = "http://192.168.3.120:8080/dev-api/robot/";
        public string Post(string data, string uri, string token)
        {
            //先根据用户请求的uri构造请求地址
            string serviceUrl = string.Format("{0}{1}?token={2}", this.BaseUri, uri, token);
            //创建Web访问对象
            HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(serviceUrl);
            //把用户传过来的数据转成“UTF-8”的字节流
            byte[] buf = System.Text.Encoding.GetEncoding("UTF-8").GetBytes(data);

            myRequest.Method = "POST";
            myRequest.ContentLength = buf.Length;
            myRequest.ContentType = "application/json";
            myRequest.MaximumAutomaticRedirections = 1;
            myRequest.AllowAutoRedirect = true;
            //token校验
            //myRequest.Headers.Add("Authorization", "Bearer " + token);
            //发送请求
            Stream stream = myRequest.GetRequestStream();
            stream.Write(buf, 0, buf.Length);
            stream.Close();

            //获取接口返回值
            //通过Web访问对象获取响应内容
            HttpWebResponse myResponse = (HttpWebResponse)myRequest.GetResponse();
            //通过响应内容流创建StreamReader对象，因为StreamReader更高级更快
            StreamReader reader = new StreamReader(myResponse.GetResponseStream(), Encoding.UTF8);
            //string returnXml = HttpUtility.UrlDecode(reader.ReadToEnd());//如果有编码问题就用这个方法
            string returnXml = reader.ReadToEnd();//利用StreamReader就可以从响应内容从头读到尾
            reader.Close();
            myResponse.Close();
            return returnXml;

        }

        public string HttpGet(string Url, string postDataStr)
        {
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url + (postDataStr == "" ? "" : "?") + postDataStr);
            request.Method = "GET";
            request.ContentType = "text/html;charset=UTF-8";

            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            Stream myResponseStream = response.GetResponseStream();
            StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.GetEncoding("utf-8"));
            string retString = myStreamReader.ReadToEnd();
            myStreamReader.Close();
            myResponseStream.Close();

            return retString;
        }

        public string GetToken(string userName, string idCard)
        {
            string url = BaseUri + "getTokenByLabour?name=" + userName + "&idCard=" + idCard;

            return GetMaping(url);
        }

        private static string GetMaping(string url)
        {
            // 这里不知道用的是哪个版本的SSL加密，所以把所有版本都填上
            ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3 | SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls;

            // 构造请求
            HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
            request.ProtocolVersion = HttpVersion.Version11;
            request.Method = "GET";
            request.ContentType = "text/html;charset=UTF-8";
            //request.Credentials = new NetworkCredential(userName, pwd);

            // 获取响应
            using (var response = request.GetResponse())
            {
                var stremReader = new StreamReader(response.GetResponseStream());
                return stremReader.ReadToEnd();
            }
        }

        public string GetUserList(string serialNo)
        {
            string url = BaseUri + "getUserList?serialNo=" + serialNo;

            return GetMaping(url);
        }

        public string GetBuildingList(string serialNo, string token)
        {
            string url = BaseUri + "getProjectInfo?serialNo=" + serialNo + "&token=" + token;

            return GetMaping(url);
        }
    }
}
